import router from '@/router'
import store from '@/store'

router.beforeEach(async(to, from, next) => {
  // console.log(55555, store.getters.token)
  if (store.getters.token) {
    // token 存在
    if (to.path === '/login') {
      next('/')
    } else {
      if (Object.keys(store.state.user.profile).length === 0) {
        await store.dispatch('user/getProfile')
      }
      next()
    }
  } else {
    //   token 不存在
    // 是否在白名单
    const whiteList = ['/login', '/404']
    if (whiteList.includes(to.path)) {
      next()
    } else {
      next('/login')
    }
  }
})
